#!/bin/bash
# Configure script for Vialinx Studio - Simple IDE
# Copyright 2008-2009 (C) Vialinx Laboratories 

FULL_PROGRAM_NAME="Vialinx Studio"
SHORT_PROGRAM_NAME="ViaStudio"
STUDIO_VERSION="0.0.1"
EDITOR_VERSION="0.0.1"

prefix="/usr"
qt4_test_dir="LinuxTest/qt4/"
qscintilla_test_dir="LinuxTest/qscintilla/"

clear() {
	rm -f Components/Editor/sources/Const.h Components/Editor/ViaEditor.pro
}

#===============================================#
#				Parse arguments					#
#===============================================#

for arg in ${@}; do
	case ${arg} in
	
	--prefix=*)
		prefix_argument=`echo ${arg} | cut -c 10-`
		;;
	
	*)
	
		echo "Unrecognized option: ${arg}"
		usage
		exit 1
		;;

	esac

done

#===============================================#
#				Initialization					#
#===============================================#

clear
rm -f configure.log

if [ -n "${prefix_argument}" ]; then
	prefix="${prefix_argument}"
fi

#===============================================#
#				Check programs					#
#===============================================#

#	make
echo -n "checking for make... "
if [ -n "${make_cmd}" ]; then
	make_cmd=`which ${make_cmd}`
else
	make_cmd=`which make`
	if [ -z "${make_cmd}" ]; then
		make_cmd=`which gmake`
	fi
fi
if [ -n "${make_cmd}" ]; then
	echo "${make_cmd}"
else
	echo "no"
	exit 1
fi

#	qmake
echo -n "checking for qmake... "
if [ -n "${arg_qmake_cmd}" ]; then
	qmake_cmd=`which ${arg_qmake_cmd}`
else
	qmake_cmd=`which qmake-qt4`
	if [ -z "${qmake_cmd}" ]; then
		qmake_cmd=`which qmake`
	fi
fi
if [ -n "${qmake_cmd}" ]; then
	echo "${qmake_cmd}"
else
	echo "no"
	exit 1
fi

#============Check Qt4 libraries============#

echo -n "checking for Qt4 libraries... "

cd ${qt4_test_dir}
${qmake_cmd} 2> configure.log
${make_cmd} 1> /dev/null 2>> configure.log

test_output=`cat configure.log`
${make_cmd} clean > /dev/null
cd ../../

if [ -z "${test_output}" ]; then
	echo "passed."
else
	echo "failed."
	echo "Please, install Qt4 libraries"
	exit 1
fi

#============Check QScintilla===============#

echo -n "checking for QScintilla libraries... "

cd ${qscintilla_test_dir}
${qmake_cmd} 2> configure.log
${make_cmd} 1> /dev/null 2>> configure.log

test_output=`cat configure.log`
${make_cmd} clean > /dev/null
cd ../../

if [ -z "${test_output}" ]; then
	echo "passed."
else
	echo "failed."
	echo "Please, install QScintilla library. You can download it from your repository"
	echo "or from official site: http://www.riverbankcomputing.co.uk/software/qscintilla/download"
	exit 1
fi

#===============================================#
#				Create files					#
#===============================================#

#=============Files with constants==============#

echo '
#ifndef VIAEDITOR_CONST_H
#define VIAEDITOR_CONST_H

#define VERSION "'${EDITOR_VERSION}'"
#define ORGANIZATION "Vialinx Laboratories"
#define PROGRAM_NAME "Vialinx Editor"

#endif
' > Components/Editor/sources/Const.h

#==================Makefile=====================#

echo "PREFIX="${prefix}"
BIN=\$(PREFIX)/bin

QMAKE="${qmake_cmd}"
MAKE="${make_cmd}"

Components/Editor/ViaEditor:
	cd Components/Editor && \$(QMAKE) && \$(MAKE)
	
	@echo \"+-----------------------------------------+\";
	@echo \"|        Compiling was successful.        |\";
	@echo \"+-----------------------------------------+\";

distclean:
	rm -f ViaEditor
	cd Components/Editor && make clean
	rm -f Components/Editor/Makefile

clean:
	cd Components/Editor && make clean
	rm -f Components/Editor/Makefile

install:
	install -d \$(BIN)
	install -m 755 ViaEditor \$(BIN)

uninstall:
	rm -f \$(BIN)/ViaEditor

" > Makefile

#=================Project files================#

echo 'TEMPLATE = app
TARGET      = ../../ViaEditor
DEPENDPATH  += .
INCLUDEPATH += .
LIBS        += -lqscintilla2
RESOURCES += ViaEditor.qrc
CONFIG += release

OBJECTS_DIR = build
MOC_DIR = build

# Input
HEADERS += \
		sources/Editor.h \
		sources/MultiEditor.h \
		sources/ColorButton.h \
		sources/AdvancedEditor.h \
		sources/Interface.h \
		sources/EditorGui.h \
		sources/SettingsDialog.h \
		sources/TabsView.h \
		sources/LexerCenter.h \
		sources/SessionsManager.h \
		sources/SessionListWidget.h \
		sources/GoToLineWidget.h \
		sources/FindReplaceWidget.h \
		sources/AboutEditorDialog.h \
		sources/InformationDialog.h

SOURCES += \
		sources/main.cpp \
		sources/Editor.cpp \
		sources/MultiEditor.cpp \
		sources/ColorButton.cpp \
		sources/AdvancedEditor.cpp \
		sources/Interface.cpp \
		sources/EditorGui.cpp \
		sources/SettingsDialog.cpp \
		sources/TabsView.cpp \
		sources/LexerCenter.cpp \
		sources/SessionsManager.cpp \
		sources/SessionListWidget.cpp \
		sources/GoToLineWidget.cpp \
		sources/FindReplaceWidget.cpp \
		sources/AboutEditorDialog.cpp \
		sources/InformationDialog.cpp
		
' > Components/Editor/ViaEditor.pro



echo "+-----------------------------------------+";
echo "|      Everything is ok. Type \"make\"      |";
echo "+-----------------------------------------+";
